package com.ruoyi.enterprise.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.enterprise.domain.EnterpriseRelationship;
import com.ruoyi.enterprise.mapper.EnterpriseInfoMapper;
import com.ruoyi.enterprise.domain.EnterpriseInfo;
import com.ruoyi.enterprise.service.IEnterpriseInfoService;

/**
 * 企业信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-24
 */
@Service
public class EnterpriseInfoServiceImpl implements IEnterpriseInfoService 
{
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;

    /**
     * 查询企业信息
     * 
     * @param enterpriseId 企业信息主键
     * @return 企业信息
     */
    @Override
    public EnterpriseInfo selectEnterpriseInfoByEnterpriseId(Integer enterpriseId)
    {
        return enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(enterpriseId);
    }

    /**
     * 查询企业信息列表
     * 
     * @param enterpriseInfo 企业信息
     * @return 企业信息
     */
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoList(EnterpriseInfo enterpriseInfo)
    {
        return enterpriseInfoMapper.selectEnterpriseInfoList(enterpriseInfo);
    }

    /**
     * 新增企业信息
     * 
     * @param enterpriseInfo 企业信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEnterpriseInfo(EnterpriseInfo enterpriseInfo)
    {
        enterpriseInfo.setCreateTime(DateUtils.getNowDate());
        int rows = enterpriseInfoMapper.insertEnterpriseInfo(enterpriseInfo);
        insertEnterpriseRelationship(enterpriseInfo);
        return rows;
    }

    /**
     * 修改企业信息
     * 
     * @param enterpriseInfo 企业信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEnterpriseInfo(EnterpriseInfo enterpriseInfo)
    {
        enterpriseInfo.setUpdateTime(DateUtils.getNowDate());
        enterpriseInfoMapper.deleteEnterpriseRelationshipByEnterpriseId(enterpriseInfo.getEnterpriseId());
        insertEnterpriseRelationship(enterpriseInfo);
        return enterpriseInfoMapper.updateEnterpriseInfo(enterpriseInfo);
    }

    /**
     * 批量删除企业信息
     * 
     * @param enterpriseIds 需要删除的企业信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEnterpriseInfoByEnterpriseIds(Integer[] enterpriseIds)
    {
        enterpriseInfoMapper.deleteEnterpriseRelationshipByEnterpriseIds(enterpriseIds);
        return enterpriseInfoMapper.deleteEnterpriseInfoByEnterpriseIds(enterpriseIds);
    }

    /**
     * 删除企业信息信息
     * 
     * @param enterpriseId 企业信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEnterpriseInfoByEnterpriseId(Integer enterpriseId)
    {
        enterpriseInfoMapper.deleteEnterpriseRelationshipByEnterpriseId(enterpriseId);
        return enterpriseInfoMapper.deleteEnterpriseInfoByEnterpriseId(enterpriseId);
    }

    /**
     * 新增企业关系信息
     * 
     * @param enterpriseInfo 企业信息对象
     */
    public void insertEnterpriseRelationship(EnterpriseInfo enterpriseInfo)
    {
        List<EnterpriseRelationship> enterpriseRelationshipList = enterpriseInfo.getEnterpriseRelationshipList();
        Integer enterpriseId = enterpriseInfo.getEnterpriseId();
        if (StringUtils.isNotNull(enterpriseRelationshipList))
        {
            List<EnterpriseRelationship> list = new ArrayList<EnterpriseRelationship>();
            for (EnterpriseRelationship enterpriseRelationship : enterpriseRelationshipList)
            {
                enterpriseRelationship.setEnterpriseId(enterpriseId);
                list.add(enterpriseRelationship);
            }
            if (list.size() > 0)
            {
                enterpriseInfoMapper.batchEnterpriseRelationship(list);
            }
        }
    }
}
